class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        # 创建一个哈希表存储值和索引
        hash_map = {}

        # 遍历数组中的每个元素
        for i, num in enumerate(nums):
            # 计算当前元素与目标值的差值
            difference = target - num

            # 检查哈希表中是否已经存在该差值
            if difference in hash_map:
                # 如果存在，返回差值的索引和当前元素的索引
                return [hash_map[difference], i]

            # 如果差值不存在，将当前元素及其索引存入哈希表
            hash_map[num] = i


# 示例测试用例
if __name__ == "__main__":
    s = Solution()
    print(s.twoSum([2, 7, 11, 15], 9) == [0, 1])  # 输出: [0, 1]
    print(s.twoSum([3, 2, 4], 6) == [1, 2])  # 输出: [1, 2]
    print(s.twoSum([3, 3], 6) == [0, 1])  # 输出: [0, 1]
